package com.oot2d.ui;

import com.oot2d.entity.Movable;
import com.oot2d.entity.Player;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import com.oot2d.util.File;
import com.oot2d.util.InputManager;

public class HUD {
    private Player link;
    private int maxHearts;
    private int wholeHearts;
    private float remainder;
    private float maxMagic;
    private int magicWidth;

    private Image emptyHeart;
    private Image wholeHeart;
    private Image quarterHeart;
    private Image halfHeart;
    private Image threeHeart;

    private Image magicSmall;
    private Image magicLarge;

    private Image buttons;
    private Image kokiriSword;

    private static Movable mover;
    private static int changingItem = -1;
    private int citems[];

    public HUD(Player p)
    {
        link = p;

        try {
            magicSmall = new Image("res/ui/magic_small.png", false, Image.FILTER_NEAREST);
            magicLarge = new Image("res/ui/magic_large.png", false, Image.FILTER_NEAREST);
            emptyHeart = new Image("res/ui/heart_empty.png", false, Image.FILTER_NEAREST);
            wholeHeart = new Image("res/ui/heart_full.png", false, Image.FILTER_NEAREST);
            quarterHeart = new Image("res/ui/heart_one.png", false, Image.FILTER_NEAREST);
            halfHeart = new Image("res/ui/heart_half.png", false, Image.FILTER_NEAREST);
            threeHeart = new Image("res/ui/heart_three.png", false, Image.FILTER_NEAREST);

            buttons = new Image("res/ui/buttons.png", false, Image.FILTER_NEAREST);
            kokiriSword = new Image("res/ui/sword_kokiri.png", false, Image.FILTER_NEAREST);

        } catch (SlickException e) {
            System.err.println(e.getMessage());
        }

        mover = new Movable();
        citems = new int[] {-1, -1, -1};
    }

    public static void changeItem(int itemSlot, int c)
    {
        changingItem = itemSlot;
        mover.setPos(((itemSlot % 6) * 59.0f) + 79.0f, ((itemSlot / 6) * 50.0f) + 71.0f);

        if (c == 0)
            mover.moveTo(402.0f, 4.0f, 1.0f);

        else if (c == 1)
            mover.moveTo(426.0f, 19.0f, 1.0f);

        else if (c == 2)
            mover.moveTo(450.0f, 4.0f, 1.0f);
    }

    public void update(int d)
    {
        if (mover.isMoving()) {
            InputManager.clearKeys();
            mover.update(d);
        } else {
            for (int i = 0; i < 3; i++)
                citems[i] = Items.getCItemSlot(i);
        }

        maxHearts = (int)link.getMaxHealth();
        wholeHearts = (int)link.getHealth();
        remainder = link.getHealth() % 1;

        maxMagic = link.getMaxMagic();
        magicWidth = Math.round(((maxMagic - link.getMagic()) / maxMagic) * (maxMagic == 50.0f ? 57.0f : 114.0f));
    }

    public void render(Graphics g)
    {
        //Render all whole hearts
        float y = 5.0f;
        float x;

        for (int i = 0; i < maxHearts; i++) {
            x = 5.0f + ((i > 9 ? i - 10 : i) * 12.0f);

            if (i == wholeHearts && remainder != 0) {
                if (remainder == 0.25f)
                    quarterHeart.draw(x, y);
                else if (remainder == 0.5f)
                    halfHeart.draw(x, y);
                else
                    threeHeart.draw(x, y);
            }

            else if (i > (wholeHearts - 1))
                emptyHeart.draw(x, y);

            else
                wholeHeart.draw(x, y);

            if (i == 9)
                y += 11.0f;
        }

        y += 13.0f;

        //Render magic bar
        if (maxMagic == 50.0f)
            magicSmall.draw(5.0f, y);
        else
            magicLarge.draw(5.0f, y);

        g.setColor(new Color(41, 41, 41));
        g.fillRect((maxMagic == 50.0f ? 64.0f : 121.0f) - magicWidth, y + 2, magicWidth, 6);

        //A, B, and C buttons
        buttons.draw(355.0f, 5.0f);

        //Sword icon
        if (File.sword == 1)
            kokiriSword.draw(355.0f, 5.0f);

        //C-button items
        if (citems[0] != -1)
            Items.drawItem(citems[0], 402.0f, 4.0f);

        if (citems[1] != -1)
            Items.drawItem(citems[1], 426.0f, 19.0f);

        if (citems[2] != -1)
            Items.drawItem(citems[2], 450.0f, 4.0f);

        if (mover.isMoving())
            Items.drawItem(changingItem, mover.x, mover.y);
    }
}
